package cn.tjie.basic.controller;

import cn.tjie.basic.dto.CodeDto;
import cn.tjie.basic.exception.BusinessException;
import cn.tjie.basic.service.IVerifyCodeService;
import cn.tjie.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return new AjaxResult(base64Str,true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试");
        }
    }
    /**
     * 获取手机验证码接口
     * @param
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult phoneVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.phoneVerifyCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            //后端打印错误日志
            //e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 微信登录时，发送手机验证码
     * @param codeDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.binderSmsCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            //后端打印错误日志
            //e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
